package com.example.ice.fittime.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.support.v4.graphics.drawable.RoundedBitmapDrawable;
import android.support.v4.graphics.drawable.RoundedBitmapDrawableFactory;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.target.BitmapImageViewTarget;
import com.example.ice.fittime.R;
import com.example.ice.fittime.entry.ArticleBanner;
import com.example.ice.fittime.entry.ExperienceShare;
import com.example.ice.fittime.entry.Topic;
import com.example.ice.fittime.entry.UserActivity;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * Created by Administrator on 2016/10/13.
 */

public class HomeRecyclerAdapter extends RecyclerView.Adapter<HomeRecyclerAdapter.ViewHolder> {
    private List<Object> dataList;
    private Context context;
    private int ids[];
    private HomePagerAdapter homePagerAdapter;
    private List<ImageView> imageViewList;
    public HomeRecyclerAdapter(List<Object> dataList, Context context) {
        this.dataList = dataList;
        this.context = context;
        ids = new int[]{R.layout.home_articlebanners,R.layout.home_bbs,R.layout.home_topic,R.layout.home_share};
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(viewType, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, int position) {
        if (position == 0) {
            List<ArticleBanner> bannerList = (List<ArticleBanner>) dataList.get(position);
            imageViewList = new ArrayList<>();
            for(int i=0;i<2*bannerList.size();i++) {
                ImageView imageView = new ImageView(context);
                Glide.with(context).load(bannerList.get(i%bannerList.size()).getImage_url()).into(imageView);
                imageViewList.add(imageView);
            }
            homePagerAdapter = new HomePagerAdapter(context,imageViewList);
            holder.home_viewPager.setAdapter(homePagerAdapter);
            holder.home_viewPager.setCurrentItem(5000000 / 2 / imageViewList.size() * imageViewList.size());
        } else if (position == 1) {
            List<UserActivity> userActivity = (List<UserActivity>) dataList.get(position);
            holder.bbsRecyclerView.setLayoutManager(new LinearLayoutManager(context,LinearLayoutManager.HORIZONTAL,false));
            holder.bbsRecyclerView.setAdapter(new HomeBBSRecyclerAdapter(context,userActivity));
        } else if (position == 2) {
            List<Topic> topic = (List<Topic>) dataList.get(position);
            LayoutInflater.from(context).inflate(R.layout.home_topic_sub, ((ViewGroup) holder.itemView),false);
            
            setTopic(holder, topic.get(0),0);
            setTopic(holder, topic.get(1),1);
            setTopic(holder, topic.get(2),2);
        } else if (position >= 3) {
            ExperienceShare share = (ExperienceShare) dataList.get(position);
            if (position > 3) {
                holder.homeShareHead.setVisibility(View.GONE);
            }
            Glide.with(context).load(share.getImage_url()).into(holder.shareImg);
            Glide.with(context).load(share.getUser().getAvatar_url()).asBitmap().into(new BitmapImageViewTarget(holder.shareUserIcon) {
                @Override
                protected void setResource(Bitmap resource) {
                    RoundedBitmapDrawable circularBitmapDrawable =
                            RoundedBitmapDrawableFactory.create(context.getResources(), resource);
                    circularBitmapDrawable.setCircular(true);
                    holder.shareUserIcon.setImageDrawable(circularBitmapDrawable);
                }
            });
            holder.shareUserName.setText(share.getUser().getName());
            holder.shareTitle.setText(share.getTitle());
            holder.shareDescription.setText(share.getDescription());
            holder.shareCollectionCount.setText(share.getCollection_count() + "");
            holder.shareCommentCount.setText(share.getComment_count() + "");
            holder.homeShare.setTag(1);
            holder.homeShare.setOnClickListener(share);
            holder.homeShareUser.setTag(2);
            holder.homeShareUser.setOnClickListener(share);
        }
    }

    private void setTopic(ViewHolder holder, Topic topic,int position) {
        if (position == 0) {
            View view = LayoutInflater.from(context).inflate(R.layout.home_topic_sub, ((ViewGroup) holder.itemView), false);
            ImageView topicImg = ((ImageView) view.findViewById(R.id.home_topic_img));
            TextView topicType = ((TextView) view.findViewById(R.id.home_topic_type));
            TextView topicTitle = ((TextView) view.findViewById(R.id.home_topic_title));
            TextView topicDescription = ((TextView) view.findViewById(R.id.home_topic_description));
            TextView topicCollectionCount = ((TextView) view.findViewById(R.id.home_topic_collectionCount));
            TextView topicCommentCount = ((TextView) view.findViewById(R.id.home_topic_commentCount));
            Glide.with(context).load(topic.getArticle().getImage_url()).into(topicImg);
            topicType.setText("人物故事");
            topicTitle.setText(topic.getTitle());
            topicDescription.setText(topic.getArticle().getDescription());
            topicCollectionCount.setText(topic.getArticle().getCollection_count()+"");
            topicCommentCount.setText(topic.getArticle().getComment_count()+"");
            ((ViewGroup) holder.itemView).addView(view);
        } else if (position == 1) {
            View view = LayoutInflater.from(context).inflate(R.layout.home_topic_sub, ((ViewGroup) holder.itemView), false);
            ImageView topicImg1 = ((ImageView) view.findViewById(R.id.home_topic_img));
            TextView topicType1 = ((TextView) view.findViewById(R.id.home_topic_type));
            TextView topicTitle1 = ((TextView) view.findViewById(R.id.home_topic_title));
            TextView topicDescription1 = ((TextView) view.findViewById(R.id.home_topic_description));
            TextView topicCollectionCount1 = ((TextView) view.findViewById(R.id.home_topic_collectionCount));
            TextView topicCommentCount1 = ((TextView) view.findViewById(R.id.home_topic_commentCount));
            Glide.with(context).load(topic.getArticle().getImage_url()).into(topicImg1);
            topicType1.setText("吃出好身材");
            topicTitle1.setText(topic.getTitle());
            topicDescription1.setText(topic.getArticle().getDescription());
            topicCollectionCount1.setText(topic.getArticle().getCollection_count()+"");
            topicCommentCount1.setText(topic.getArticle().getComment_count()+"");
            ((ViewGroup) holder.itemView).addView(view);
        } else if(position == 2){
            View view = LayoutInflater.from(context).inflate(R.layout.home_topic_sub, ((ViewGroup) holder.itemView), false);
            ImageView topicImg2 = ((ImageView) view.findViewById(R.id.home_topic_img));
            TextView topicType2 = ((TextView) view.findViewById(R.id.home_topic_type));
            TextView topicTitle2 = ((TextView) view.findViewById(R.id.home_topic_title));
            TextView topicDescription2 = ((TextView) view.findViewById(R.id.home_topic_description));
            TextView topicCollectionCount2 = ((TextView) view.findViewById(R.id.home_topic_collectionCount));
            TextView topicCommentCount2 = ((TextView) view.findViewById(R.id.home_topic_commentCount));
            Glide.with(context).load(topic.getArticle().getImage_url()).into(topicImg2);
            topicType2.setText("官方商城");
            topicTitle2.setText(topic.getTitle());
            topicDescription2.setText(topic.getArticle().getDescription());
            topicCollectionCount2.setText(topic.getArticle().getCollection_count()+"");
            topicCommentCount2.setText(topic.getArticle().getComment_count()+"");
            ((ViewGroup) holder.itemView).addView(view);
        }

    }

    @Override
    public int getItemCount() {
        return dataList.size();
    }


    @Override
    public int getItemViewType(int position) {
        if (position < 3) {
            return ids[position];
        }else {
            return ids[ids.length-1];
        }
    }

    public void add(int index, Object object) {
        dataList.add(index,object);
        notifyItemInserted(index);
    }

    public void addAll(Collection collection) {
        dataList.addAll(collection);
        notifyItemRangeInserted(dataList.size(),collection.size());
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        private  ViewPager home_viewPager;
        private  TextView experience;
        private  TextView body;
        private  TextView mall;
        private  ImageView shareImg;
        private  ImageView shareUserIcon;
        private  TextView shareUserName;
        private  TextView shareTitle;
        private  TextView shareDescription;
        private  TextView shareCommentCount;
        private  TextView shareCollectionCount;
        private  RecyclerView bbsRecyclerView;
        private  RelativeLayout bbsHead;
        private  TextView homeShareHead;
        private  LinearLayout homeShareUser;
        private  LinearLayout homeShare;

        public ViewHolder(View itemView) {
            super(itemView);
            home_viewPager = ((ViewPager) itemView.findViewById(R.id.home_viewPager));
            experience = ((TextView) itemView.findViewById(R.id.experience));
            body = ((TextView) itemView.findViewById(R.id.body));
            mall = ((TextView) itemView.findViewById(R.id.mall));

            bbsRecyclerView = ((RecyclerView) itemView.findViewById(R.id.home_bbs_recyclerView));
            bbsHead = ((RelativeLayout) itemView.findViewById(R.id.home_bbs_head));

            homeShare = ((LinearLayout) itemView.findViewById(R.id.home_share));
            homeShareUser = ((LinearLayout) itemView.findViewById(R.id.home_share_user));
            homeShareHead = ((TextView) itemView.findViewById(R.id.home_share_head));

            shareImg = ((ImageView) itemView.findViewById(R.id.home_share_img));
            shareUserIcon = ((ImageView) itemView.findViewById(R.id.home_share_userIcon));
            shareUserName = ((TextView) itemView.findViewById(R.id.home_share_userName));
            shareTitle = ((TextView) itemView.findViewById(R.id.home_share_title));
            shareDescription = ((TextView) itemView.findViewById(R.id.home_share_description));
            shareCommentCount = ((TextView) itemView.findViewById(R.id.home_share_commentCount));
            shareCollectionCount = ((TextView) itemView.findViewById(R.id.home_share_collectionCount));
        }

    }

}
